package com.wjj.happytime.activity.listen_activity;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.support.v7.widget.Toolbar;
import android.test.mock.MockApplication;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import com.jiongbull.jlog.JLog;
import com.wjj.happytime.R;
import com.wjj.happytime.adapter.listen_frg_adapter.NomalRadioAdapter;
import com.wjj.happytime.application.BaseApplication;
import com.wjj.happytime.entity.listen_entity.HomePageRadioDetail;
import com.wjj.happytime.entity.listen_entity.ListenRadios;
import com.wjj.happytime.entity.listen_entity.RadioDetail;
import com.wjj.happytime.fragment.happy_frg.CommonHappyFrg;
import com.wjj.happytime.my_interface.HttpService;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NomalRadioListActivity extends AppCompatActivity implements OnClickListener,Callback<ListenRadios>{

  public Toolbar toolBar;
  public TextView tvTitle;
  public RecyclerView rvNomal;
  public int type;
  public int radioType=0;
  public int page=1;
  public int pageSize=20;
  public int provinceCode=370000;
  public ActionBar actionBar;
  public HttpService service;
  public NomalRadioAdapter adapter;
  public List<HomePageRadioDetail> topRadios;
  public int total;
  private boolean isLoading;
  private boolean isFrist;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nomal_radio_list);
    initView();
    initData();
    initEvent();
  }

  private void initEvent() {
    rvNomal.addOnScrollListener(new OnScrollListener() {
      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        LinearLayoutManager layout = (LinearLayoutManager) recyclerView.getLayoutManager();
        //总条目
        int totalCount = layout.getItemCount();
        //最后一个可见的条目位置
        int lastVisibleItemPosition = layout.findLastVisibleItemPosition();
        if (!isLoading&&lastVisibleItemPosition+1==totalCount&&totalCount<total) {
          isLoading=true;
          service.getRadioData(++page,pageSize,provinceCode,radioType).enqueue(NomalRadioListActivity.this);
        }
      }
    });

  }

  private void initView() {
    toolBar = ((Toolbar) findViewById(R.id.toolbar_nomal_radio));
    tvTitle = ((TextView) findViewById(R.id.tv_title));
    rvNomal = ((RecyclerView) findViewById(R.id.rv_nomal_radio));

    setSupportActionBar(toolBar);
    actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
  }

  private void initData() {
    Intent intent = getIntent();
    //5:排行榜 9:收听历史
    type = intent.getIntExtra("type", -1);
    topRadios = (List<HomePageRadioDetail>) intent.getSerializableExtra("topRadios");
    switch (type) {
      case R.id.tv_radio_local:
        tvTitle.setText("本地台");
        radioType=2;
        break;
      case R.id.tv_radio_country:
        tvTitle.setText("国家台");
        radioType=1;
        break;
      case R.id.tv_radio_internet:
        tvTitle.setText("网络台");
        radioType=3;
        break;
      case 5://排行
        tvTitle.setText("排行榜");
        adapter=new NomalRadioAdapter(this,null);
        rvNomal.setAdapter(adapter);
        adapter.setTopRadios(topRadios);
        adapter.notifyDataSetChanged();
        break;
      case 9://收听历史
        tvTitle.setText("收听历史");
        break;
    }
    if (radioType!=0) {
      getRadioData();
    }
  }

  //请求网络数据
  private void getRadioData() {
    service = ((BaseApplication) getApplication()).getService();
    service.getRadioData(page,pageSize,provinceCode,radioType).enqueue(this);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        onBackPressed();
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public void onClick(View view) {
    int id = view.getId();
    switch (id) {

    }
  }

  @Override
  public void onResponse(Call<ListenRadios> call, Response<ListenRadios> response) {
    JLog.e("http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum="+page+"&pageSize=20&provinceCode="+provinceCode+"&radioType="+radioType);
    List<RadioDetail> result = response.body().getResult();
    isLoading=false;
    if (!isFrist) {
      isFrist=true;
      total = response.body().getTotal();
      JLog.e(total+"");
      adapter = new NomalRadioAdapter(NomalRadioListActivity.this,result);
      rvNomal.setAdapter(adapter);
    }else {
      adapter.addDatas(result);
    }
  }

  @Override
  public void onFailure(Call<ListenRadios> call, Throwable t) {
    isLoading=false;
  }
}
